<?php
namespace app\taiyuan\controller;
use think\Db;

class Yongjin extends Base
{
	public function _initialize()
	{
		// 调用父类前置方法
		parent::_initialize();

		// 登录校验
		$this->IsLogin();
	}
	
	
	public static $status_array = [
		3 => ['value' => 3, 'name' => '未处理'],
		1 => ['value' => 1, 'name' => '已审核'],
		2 => ['value' => 2, 'name' => '已驳回'],
	];

	public function index()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		if(request()->isPost())
		{
			$params = input();
			if(!empty($params['page']) && !empty($params['limit']))
			{
				$map = [];
				if(isset($params['status']) && $params['status'] != '')
				{
					$map['status'] = $params['status'];
				}
					$map['status2'] = 1;
				$count = Db::name('yj_tixian')->where($map)->count();
				$list_data = Db::name('yj_tixian')->where($map)->order('id desc')->limit(($params['page']-1)*$params['limit'],$params['limit'])->select();
				if(!empty($list_data))
				{
					//打款状态
					$status_array = self::$status_array;
					//遍历处理
					foreach($list_data as $key => $value)
					{
						//审核url
						$list_data[$key]['audit_url'] = url('yongjin/auditinfo', ['id' => $value['id']]);
					    
					    //提现申请时间
					    $list_data[$key]['add_time_time'] = !empty($value['add_time']) ? date('Y-m-d H:i:s', $value['add_time']) : '';

					    //打款状态
					    $list_data[$key]['status_name'] = !empty($status_array[$value['status']]['name']) ? $status_array[$value['status']]['name'] : '';
					
					    $list_data[$key]['type_name']=$value['type']==1?'支付宝':'银行';
					    
					    
					    $list_data[$key]['khtypetext']=$value['khtype']==1?'客户':'团队';
					}
					$result['code'] = 0;
	        		$result['msg'] = "请求成功";
	        		$result['data'] = $list_data;
	        		$result['count'] = $count;
				}else
				{
					$result['code'] = -1;
	        		$result['msg'] = "暂无数据";
				}
			}
			echo json_encode($result);
		}else
		{
			//打款状态
			$status_array = self::$status_array;
			$this->assign('status_array', $status_array);
			return $this->fetch();
		}
	}

	/**
	 * 提现审核页面 
	 */
	public function auditinfo()
	{
		// 参数
		$params = input();
		if(!empty($params['id']))
		{
			//查询
			$info = Db::name('yj_tixian')->where(['id' => $params['id']])->find();
			if(!empty($info))
			{
				$this->assign('info',$info);
				return $this->fetch();
			}
		}else
		{
			return "缺少参数";
		}
	}

	public function audit()
	{
		$result['code'] = -1;
		$result['msg'] = '非法请求';
		if(request()->isPost())
		{
			$params = input();
			$validate = $this->validate(
				$params,
				[
					'id' => 'require',
					'status' => 'require'
				],
				[
					'id.require' => '提现id有误',
					'status.require' => '请选择审核状态'
				]
			);
			if(true !== $validate){
				echo json_encode(['code' => -1, 'msg' => $validate]);
				exit();
			}
			// 获取提现数据
			$info = Db::name('yj_tixian')->where(['id' => $params['id']])->find();
			if(empty($info))
			{
				echo json_encode(['code' => -10, 'msg' => '提现数据不存在或已删除']);
				exit();
			}
			// 状态
			if($info['status'] != 3)
			{
				echo json_encode(['code' => -11, 'msg' => '状态不可操作']);
				exit();
			}
			
			// 开始处理
        	Db::startTrans();
			// 数据处理
			if($params['status'] == 1)
			{
				//同意
				$cash_upd_data['status']=1;
				
				

			}else
			{
				//拒绝
				$cash_upd_data['status']=2;
				if($info['khtype']==1){
				    if(!Db::name('user')->where(['id' => $info['user_id']])->setInc('yongjin',$info['price']))
                	{
                		Db::rollback();
                		echo json_encode(['code' => -100, 'msg' => '操作失败']);
        				exit();
                	}
				
				}else{
				    if(!Db::name('user')->where(['id' => $info['user_id']])->setInc('yongjin2',$info['price']))
                	{
                		Db::rollback();
                		echo json_encode(['code' => -100, 'msg' => '操作失败']);
        				exit();
                	}
				
				}
				
				$balance_t=[
				    'balance_type'=>6,
				    'order_id'=>$params['id'],
				    'identity_type'=>'user',
				    'identity_id'=>$info['user_id'],
				    'divide_into'=>0,
				    'price'=>$info['price'],
				    'no_money'=>$info['price'],
				    'yes_money'=>$info['price'],
				    'msg'=>$params['msg'],
				    'status'=>1,
				    'add_time'=>time(),
				    'upd_time'=>time(),
				    'khtype'=>$info['khtype']
			    ];
			    
				if(!Db::name('balance')->insert($balance_t))
            	{
            		Db::rollback();
            		echo json_encode(['code' => -100, 'msg' => '操作失败']);
    				exit();
            	}
				
			}
			// 提现更新
			$cash_upd_data['reason'] = empty($params['msg']) ? '' : $params['msg'];
			
        	if(!Db::name('yj_tixian')->where(['id' => $params['id']])->update($cash_upd_data))
        	{
        		Db::rollback();
        		echo json_encode(['code' => -100, 'msg' => '提现申请操作失败']);
				exit();
        	}


			// 处理成功
        	Db::commit();
        	echo json_encode(['code' => 0, 'msg' => '操作成功']);
		}
	}
}